<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/6/13
 * Time: 15:00
 */

namespace app\common\model;


use think\Model;

class Article extends Model
{
    protected $table='up_article';
    private static $instance;
    //消息分类
    public static $a_type = [
        1   =>   '帮帮',
        2   =>   '走失',
        3   =>   '福利',
        4   =>   '悬赏'
    ];
    //使用权限
    public static $a_auth_type = [
        1   =>   '无',
        2   =>   '铜牌召集令',
        3   =>   '银牌召集令',
        4   =>   '金牌召集令'
    ];
    
    /**
     * 插入数据
     * @param $data
     * @return false|int
     */
    public function creatArticle($data){
        $res = $this->allowField(true)->save($data);
        return $res;
    }
    
    /**
     * 判断该用户在24小时内是否有发布过同类的消息
     * @param $where
     * @return array|false|\PDOStatement|string|Model
     */
    public function isPublished($where){
        $model = self::instance();
        $createtime = $model->where($where)->order('a_createtime desc')->column('a_createtime');
        return $createtime;
    }

    /**
     * @param $page
     * @param $field
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     * 搜索
     */
    public function selectArticleList($page,$field='*',$where,$size,$sheng,$city_id){
        $model = self::instance();
        if($size==1){
            $num = 3;
        }else{
            $num = 10;
        }
        $pageNum = $page-1;
        $list = $model->alias('a')
                            ->join('up_u_user uuu','uuu.user_id = a.a_user_id','left')
                            ->where($where)
                            ->where(function ($query1) use($sheng,$city_id){
                                $query1->where(function ($query){
                                    $query->where('a_auth_type', 4);

                                })
                                ->whereOr(function ($query) use($sheng,$city_id) {
                                    $query->where('a_auth_type', 1)
                                        ->where('a_shi', $city_id)
                                        ->where('a_sheng',$sheng);
                                })
                                    ->whereOr(function ($query) use($sheng,$city_id){
                                        $query->where('a_auth_type', 2)
                                            ->where('a_auth_city', $city_id);
                                    })
                                    ->whereOr(function ($query) use($sheng,$city_id){
                                        $query->where('a_auth_type', 3)
                                            ->where('a_auth_city',$sheng);
                                    });

                            })

                            ->field($field)
                            ->order('a.a_createtime desc')
                            ->limit($pageNum*$num,$num)
                            ->select();
        return $list;
    }

    /***
     * @param string $field
     * @param $where
     * @return array|false|\PDOStatement|string|Model
     * 查询详情
     */
    public function selectArticleOne($field='*',$where){
        $model = self::instance();
        $list = $model->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.a_user_id','left')
            ->where($where)
            ->field($field)
            ->find();
        return $list;
    }
    
    /**
     * 展示消息列表&详细信息
     * @param string $field  需要查询的字段
     * @param string $where  查询条件  详细信息时不需要
     * @param string $pageSize  每页的记录数
     * @param string $id  查看详细信息时的id值
     * @return array|false|\PDOStatement|string|Model|\think\Paginator
     */
    public function getArticleInfo($field = '*', $where = '', $pageSize = '', $id = ''){
        $model = self::instance();
        if($id == ''){//查询列表
            $lists = $model->alias('a')
                ->join('up_u_user uuu', 'uuu.user_id = a.a_user_id', 'left')
                ->join('up_area ua1', 'ua1.district_id = a.a_shi', 'left')
                ->join('up_area ua2', 'ua2.district_id = a.a_sheng', 'left')
                ->join('up_area ua3', 'ua3.district_id = a.a_auth_city', 'left')
                ->field($field)
                ->where($where)
                ->order('a_id desc')
                ->paginate($pageSize);
        }else{//单条数据的详细信息
            $lists = $model->alias('a')
                ->join('up_u_user uuu', 'uuu.user_id = a.a_user_id', 'left')
                ->join('up_area ua1', 'ua1.district_id = a.a_shi', 'left')
                ->join('up_area ua2', 'ua2.district_id = a.a_sheng', 'left')
                ->join('up_area ua3', 'ua3.district_id = a.a_auth_city', 'left')
                ->join('up_report ur', 'ur.report_aid = a.a_id', 'left')
                ->field($field)
                ->where('a_id', $id)
                ->find();
        }
        return $lists;
    }
    
    public static function instance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}